<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div id="app">
      <h2>vue-router实现路由</h2>

      <!-- router-link相当于是a标签。用于改变url -->
      <nav>
        <router-link to="/">Go to Home</router-link>
        <router-link to="/about">Go to About</router-link>
      </nav>

      <!-- router-view相当于是存放切换内容的容器 -->
      <main>
        <router-view />
      </main>
    </div>

    <script src="../vue3.js"></script>
    <script src="https://unpkg.com/vue-router@4.4.5/dist/vue-router.global.js"></script>

    <script>
      const { createApp } = Vue;
      const { createMemoryHistory, createRouter } = VueRouter;

      const HomeView = {
        template: "<div>这是首页</div>",
      };
      const AboutView = {
        template: "<div>这是关于页</div>",
      };

      // 相当于是创建了路由表
      const routes = [
        { path: "/", component: HomeView },
        { path: "/about", component: AboutView },
      ];

      // 根据路由表来生成一个路由实例
      const router = createRouter({
        history: createMemoryHistory(),
        routes: routes,
      });

      const app = createApp({});

      // app实例去使用路由实例
      app.use(router);

      app.mount("#app");
    </script>
  </body>
</html>
